package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.CarrierService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;

@RestController
@RequestMapping("/carrier")
public class CarrierController {

    @Autowired
    private CarrierService carrierService;

    // 注册
    @PostMapping("/add")
    public CommonResponse addCarrier(@RequestParam String name, @RequestParam String info, @RequestParam String password) {
        boolean res = carrierService.addCarrier(name, info, password);
        if (res) {
            return CommonResponse.ok(res);
        }
        return CommonResponse.fail("1001", new Exception("error"));
    }

    // 验证
    @PostMapping("/check")
    public CommonResponse checkCarrier(@RequestParam int id, @RequestParam String password) throws ContractException {
        boolean res = carrierService.checkCarrier(id, password);
        if (res) {
            return CommonResponse.ok(res);
        }
        return CommonResponse.fail("1001", new Exception("error"));
    }

    // 查询自己所能操作的物流信息列表
    @GetMapping("/shipments")
    public CommonResponse getShipmentsByCarrier(@RequestParam int id) throws ContractException {
        BigInteger[] res = carrierService.getShipmentsByCarrier(id);
        if (res != null) {
            return CommonResponse.ok(res);
        }
        return CommonResponse.fail("1001", new Exception("error"));
    }
}